package cn.edu.cug.cs.gtl.docsrv.auth.dao.imp;

import cn.edu.cug.cs.gtl.docsrv.auth.dao.AuthDao;
import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthRole;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthRoleMapper;

import java.math.BigDecimal;

public class AuthDaoImpl implements AuthDao {
    @Override
    public String getAuthUserFilters(BigDecimal userID) {
        Session session = Configuration.getSession();
        AuthRoleMapper authRoleMapper = session.getMapper(AuthRoleMapper.class);
        AuthRole authRole = authRoleMapper.selectByPrimaryKey(userID);
        String roleFilters = authRole.getRoleFilters();
        session.close();
        return roleFilters;
    }

    @Override
    public String getAuthUserOperators(BigDecimal userID) {
        Session session = Configuration.getSession();
        AuthRoleMapper authRoleMapper = session.getMapper(AuthRoleMapper.class);
        AuthRole authRole = authRoleMapper.selectByPrimaryKey(userID);
        String roleOperators = authRole.getRoleOperators();
        session.close();
        return roleOperators;
    }

    @Override
    public String getAuthUserResources(BigDecimal userID) {
        Session session = Configuration.getSession();
        AuthRoleMapper authRoleMapper = session.getMapper(AuthRoleMapper.class);
        AuthRole authRole = authRoleMapper.selectByPrimaryKey(userID);
        String roleResources = authRole.getRoleResources();
        session.close();
        return roleResources;
    }

    @Override
    public String getAuthUserName(BigDecimal userID) {
        Session session = Configuration.getSession();
        AuthRoleMapper authRoleMapper = session.getMapper(AuthRoleMapper.class);
        AuthRole authRole = authRoleMapper.selectByPrimaryKey(userID);
        String roleName = authRole.getRoleName();
        session.close();
        return roleName;
    }

    @Override
    public BigDecimal getAuthUserId(String userName) {
        Session session = Configuration.getSession();
        AuthRoleMapper authRoleMapper = session.getMapper(AuthRoleMapper.class);
        AuthRole authRole = authRoleMapper.selectByRoleName(userName);
        BigDecimal roleId = authRole.getRoleInternalId();
        session.close();
        return roleId;
    }
}
